package worker;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import data.Data;

public class InsertUrlCraw extends Thread {
	long timeout;
	Thread[] group;
	BasicDBObject obj;

	public InsertUrlCraw(long timeout, Thread[] group, BasicDBObject obj) {
		this.timeout = timeout;
		this.group = group;
		this.obj = obj;
		this.start();
	}

	@SuppressWarnings({ "deprecation", "static-access" })
	public void run() {
		try {
			this.sleep(timeout);
			setUp();
			DBCollection coll = Data.data("urlcraw");
			DBObject check = coll.findOne(new BasicDBObject("url", obj.get("url")));
			
			if (check == null){
				if(obj.get("url") != null){
					coll.insert(obj);
					createProducerAndSendAMessage(obj.get("url").toString());
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			for (int i = 0; i < group.length; i++) {
				try {
					group[i].suspend();
					group[i].stop();
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}

		}

	}
	private static ActiveMQConnectionFactory connectionFactory;
    private static Connection connection =null;
    private static Session session;
    private static Destination destination;
    private static boolean transacted = false;
	private static void setUp() throws JMSException, IOException {
		ResourceBundle resource = ResourceBundle.getBundle("config");		
         connectionFactory = new ActiveMQConnectionFactory(
                 ActiveMQConnection.DEFAULT_USER,
                 ActiveMQConnection.DEFAULT_PASSWORD,
                 "tcp://"+resource.getString("server")+":61616");
         if(connection == null)
         {
	         connection = connectionFactory.createConnection();
	         connection.start();
         }
         session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
         
         destination = session.createQueue(resource.getString("queue_get_content"));
     }
 
     private static void createProducerAndSendAMessage(String str) throws JMSException {
   	  
         MessageProducer producer = session.createProducer(destination);
         producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
         TextMessage message = session.createTextMessage(str);
         System.out.println("Sending message: " + message.getText());
         producer.send(message);
//         connection.close();
     }
}
